package com.e1xp.deport_management_system.controller;

import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.service.IAnalyseService;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Map;

/**
 * @Author E1xP@foxmail.com
 * @Version 1.0
 * @PACKAGE_NAME com.e1xp.deport_management_system.controller
 * @CLASS_NAME AnalyseController
 * @Description TODO 数据分析Controller
 * @Date 2021/5/17 下午 1:24
 **/
@Controller
@RequestMapping("/analysis")
public class AnalyseController {

    @Resource
    IAnalyseService analyseService;

    /**
     * 用于获取统计数据
     *
     * @param instanceId 实例id
     * @param startTime  开始时间
     * @param endTime    结束事件
     * @return AjaxResponse
     * @throws CustomException 当结束时间早于开始时间时抛出该异常
     */
    @PostMapping("/getData")
    @ResponseBody
    public AjaxResponse getData(@SessionAttribute long instanceId, LocalDate startTime, LocalDate endTime) throws CustomException {
        Map<String, Object> analyseData = analyseService.getData(instanceId, startTime, endTime);
        return AjaxResponse.success(analyseData);
    }

    /**
     * 用于渲染数据页面
     *
     * @return ModelAndView
     */
    @RequestMapping("/analysisPage")
    public ModelAndView analysisPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("analysis");
        return modelAndView;
    }

}
